package cn.hfatec.shms.system.userRole.service;

import cn.hfatec.shms.advice.annotation.MysqlDataSource;
import cn.hfatec.shms.system.userRole.dao.UserRoleDao;
import cn.hfatec.shms.system.userRole.model.UserRole;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;



/**
 *
 * @author hfatec
 * @version 1.0
 *
 * the JAVA code is generate by middlegen
 *
 */
@Service("UserRoleService")
public class UserRoleService {

    //注入Dao层
    @Resource(name = "UserRoleDao")
    private UserRoleDao userRoleDao;


    /**
     *
     * 查询已分配角色
     *
     * @param userId
     * @return
     */
    @Transactional
    @MysqlDataSource
    public List queryUserRoleByUserId(String userId) {
        return userRoleDao.queryUserRoleByUserId(userId);
    }


    /**
     *
     * 分配用户角色
     *
     * @param userId
     * @param ids
     */
    @Transactional
    @MysqlDataSource
    public void assignRole(String userId, String[] ids) {
        userRoleDao.deleteUserRoleBatch(userId);
        if(ids == null) {
            ids = new String[]{};
        }
        for(String roleId : ids) {
            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(roleId);
            userRoleDao.insert(userRole);
        }
    }
}